package com.aoxin.controller.pay.weiXinNotify;

import com.aoxin.model.PayInfo;
import com.aoxin.model.SlfRecharge;
import com.aoxin.service.inter.pay.PaymentService;
import com.aoxin.util.*;
import com.aoxin.util.weiXinPay.HttpRequestor;
import com.aoxin.util.weiXinPay.WeiXinPay;
import com.aoxin.util.weiXinPay.WeiXinPayConfig;
import com.aoxin.util.weiXinPay.xmlUntil.XMLParser;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xml.sax.SAXException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Author: 李贵
 * @Date: Created in 16:04 2018/5/7
 * @Content:
 */
@RestController
@RequestMapping("/web/slf/")
public class WeiPayTestController {


//
//    @Autowired
//    private PaymentService paymentService;


    /**
     * 微信公众号支付
     * @param body
     * @param money
     * @param userId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("pay")
    public Map<Object,Object> pay(String body, Integer money,String userId,HttpServletRequest request, HttpServletResponse response){
        Map<Object,Object> data= new HashMap<>();
        try {
            HttpSession session = request.getSession();
            String openid = (String) session.getAttribute("openid");
            if(openid==null){
                throw new IllegalArgumentException("openId为空");
            }
            WeiXinPay weiXinPay = new WeiXinPay();
            data = weiXinPay.pay(body,money,null,openid,userId);
            System.out.println("微信支付:"+data);
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 微信H5支付
     * @param body
     * @param money
     * @param userId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("h5Pay")
    public Map<Object,Object> H5Pay(String ip,String body, Integer money,String userId,HttpServletRequest request, HttpServletResponse response){

        Map<Object,Object> data= new HashMap<>();
            try {
                WeiXinPay weiXinPay = new WeiXinPay();
                data = weiXinPay.H5Pay(body,money,userId,request,ip);
                System.out.println(data);
            }catch (Exception e){
                e.printStackTrace();
            }
        return data;
    }

    /**
     * 获取openid
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("getOpenid")
    public static void getOpenid(HttpServletRequest request, HttpServletResponse response){
        try {
            HttpSession session = request.getSession();
            if(session.getAttribute("openid") != null && session.getAttribute("ip") != null){
                request.getRequestDispatcher("/slf/index").forward(request, response);
            }else {
                String appid = "wx9e4a1955fd462a63";
                String secret = "f86d144a3f881ee40670f21186c553b1";
//                String ip = WeiXinPay.getIpAddr(request);
                String code = request.getParameter("code");
                String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
                //第一次请求 获取access_token 和 openid
                String  oppid = new HttpRequestor().doGet(requestUrl);
                JSONObject oppidObj =JSONObject.fromObject(oppid);
                String access_token = (String) oppidObj.get("access_token");
                String openid = (String) oppidObj.get("openid");
                System.out.println("******************获取openId"+openid);
                System.out.println("openid:"+openid);
                session.setAttribute("openid",openid);
            //    request.getRequestDispatcher("/slf/index").forward(request, response);
           //     response.sendRedirect("/slf/login");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }



}
